package com.huang.manager;

import com.huang.utils.ThreadUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 异步任务管理器
 *
 * @author Ikaros
 * @since 2025/9/5 16:04 星期五
 */
@Component
@RequiredArgsConstructor
public class AsyncManager {

    private final ScheduledExecutorService executor;

    /**
     * 执行任务
     *
     * @param task 任务
     */
    public void execute(TimerTask task) {
        executor.schedule(task, 10, TimeUnit.MILLISECONDS);
    }

    /**
     * 停止任务线程池
     */
    public void shutdown(String poolName) {
        ThreadUtils.shutdownAndAwaitTermination(executor, poolName);
    }
}
